<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">供应商ID：</span>
                <el-input placeholder="输入供应商ID" v-model.trim="supplier_id" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">供应商名称：</span>
                <el-input placeholder="输入供应商名称" v-model.trim="supplier_name" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">变动类型：</span>
                <el-select v-model="types" clearable placeholder="请选择类型">
                    <el-option label="全部" value="0" />
                    <el-option label="收益" value="1" />
                    <el-option label="提现" value="2" />
                    <el-option label="售后扣款" value="5" />
                    <el-option label="收益扣款" value="6" />
                    <el-option label="平台调整" value="7" />
                    <el-option label="转担保金额" value="8" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">时间区间：</span>
                <el-date-picker
                    v-model="area_time"
                    type="daterange"
                    unlink-panels
                    value-format="YYYY-MM-DD"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                />
            </div>
            <div class="task-one__btn task-wrap-btn clearfix">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">搜索</el-button>
                <el-button @click="clickExport">导出</el-button>
            </div>
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
        >
            <el-table-column label="供应商" min-width="200" fixed="left">
                <template #default="{ row }">
                    <p>{{ row.supplier_title }}[ID: {{ row.supplier_id }}]</p>
                </template>
            </el-table-column>
            <el-table-column label="变动类型" min-width="120">
                <template #default="{ row }">{{ type_label[row.type - 1] }}</template>
            </el-table-column>
            <el-table-column prop="old_val" label="原金额" min-width="120"></el-table-column>
            <el-table-column prop="number" label="变动金额" min-width="120"></el-table-column>
            <el-table-column prop="val" label="现金额" min-width="120"></el-table-column>
            <el-table-column prop="intro" label="备注" min-width="300"></el-table-column>
            <el-table-column prop="created_at_label" label="创建时间" width="170" fixed="right"></el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
    </div>
    <go-back />
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch } from "vue"
import { Search } from "@element-plus/icons-vue"
import OneLoad from "@mixins/one-load"
import GoBack from "@components/GoBack.vue"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getList()
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, search_btn, pubParams, pubRefreshParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

const type_label = ["收益", "提现", "充值", "退款", "售后扣款", "收益扣款", "平台调整", "转担保金额"]
function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
        supplier_id: supplier_id.value,
        so: supplier_name.value,
        s_time: area_time.value ? area_time.value[0] : "",
        l_time: area_time.value ? area_time.value[1] : "",
        type: types.value,
    }
    getdata("finance/supplierLog", params)
}

/**
 * 搜索
 * @param {String} supplier_name 名称
 * @param {String} supplier_id ID
 * @param {String} area_time 对账日期
 * @param {String} types 类型
 */
const supplier_name = ref("")
const supplier_id = ref("")
const area_time = ref("")
const types = ref("") // 类型[0:全部,1:收益,2:提现,5:售后扣款,6:收益扣款,7:平台调整,8:转担保金额]
const handleSearch = () => {
    search_btn.value = true
    pubRefreshParams(getList)
}

// 导出
const clickExport = () => {
    proxy.$axios
        .get("finance/supplierLogExport", {
            params: {
                supplier_id: supplier_id.value,
                so: supplier_name.value,
                s_time: area_time.value ? area_time.value[0] : "",
                l_time: area_time.value ? area_time.value[1] : "",
                type: types.value,
            },
            responseType: "blob",
        })
        .then(function (res) {
            let contentDisposition = res.headers["content-disposition"]
            let filename = decodeURI(contentDisposition.split("fileName=")[1] || contentDisposition.split("filename=")[1])
            let blob = new Blob([res.data], { type: res.headers["content-type"] })
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement("a")
            link.href = url
            link.download = filename
            document.body.appendChild(link)
            link.click()
            URL.revokeObjectURL(url) // 释放内存
            document.body.removeChild(link)
        })
        .catch((err) => {})
}

/**
 * 详情
 * @param {Object} objs 详情
 * @param {Boolean} drawerDetail 弹窗状态
 * @param {Array} srcList 图片预览
 */
const objs = ref({})
const srcList = ref([])
const drawerDetail = ref(false)
const clickDetail = (row) => {
    drawerDetail.value = true
    getDetail(row.danbao_id)
}
function getDetail(id) {
    proxy.$axios
        .get("supplier/danbao/detail", {
            params: {
                danbao_id: id,
            },
        })
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                if (result.have_photo == 1) {
                    for (let i = 0; i < result.photos.length; i++) {
                        srcList.value.push(proxy.imgUrl + result.photos[i])
                    }
                }
                objs.value = result
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}
</script>
<script>
export default {
    name: "SupplierFinanceCount",
}
</script>

<style lang="less" scoped></style>
